package us.icitap.timsportal.management;

import java.util.List;

import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.ui.Button;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;

import us.icitap.timsportal.business.DomainUser;
import us.icitap.timsportal.util.SearchListener;
import us.icitap.timsportal.web.custom.PNativeButton;
import us.icitap.timsportal.web.custom.PTable;
import us.icitap.timsportal.web.custom.PTextField;
import us.icitap.timsportal.web.custom.PWindow;
import us.icitap.timsportal.ws.util.ADConnection;

@SuppressWarnings("serial")
public class DomainSearchWindow extends PWindow implements Button.ClickListener{

	private PTextField searchField;
	private PNativeButton searchBtn;
	private PTable dTable;
	private BeanItemContainer<DomainUser> dContainer;
	private SearchListener searchListener;
	private ADConnection adConnection;
	
	public DomainSearchWindow(){
		setModal(true);
		setWidth("550px");
		
		searchField = new PTextField();
		searchField.setWidth("200px");
		searchField.setRequired(true);
		
		searchBtn = new PNativeButton("Kerko", (ClickListener) this);
		searchBtn.setWidth("100px");
		searchBtn.setImmediate(true);
		searchBtn.setClickShortcut(KeyCode.ENTER);
		
		dContainer = new BeanItemContainer<DomainUser>(DomainUser.class);
		dTable = new PTable("Perdoruesit", dContainer);
		dTable.setVisibleColumns(new Object[]{"accountName", "ou"});
		dTable.setColumnHeaders(new String[]{"Perdoruesi", "Struktura"});
		dTable.setPageLength(10);
		dTable.setSizeFull();
		dTable.setSelectable(true);
		dTable.setImmediate(true);
		dTable.addListener(new Property.ValueChangeListener() {
			@Override
			public void valueChange(ValueChangeEvent event) {
				Object o = event.getProperty().getValue();
				if(o==null)
					return;
				searchListener.searchFinished(o);
				DomainSearchWindow.this.getParent().removeWindow(DomainSearchWindow.this);
			}
		});
		
		HorizontalLayout hl = new HorizontalLayout();
		hl.setSpacing(true);
		hl.addComponent(searchField);
		hl.addComponent(searchBtn);
		
		VerticalLayout vl = (VerticalLayout) getContent();
		vl.setSpacing(true);
		vl.addComponent(hl);
		vl.addComponent(dTable);
	}

	@Override
	public void buttonClick(ClickEvent event) {
		if(!searchField.isValid())
			return;
		String[] vals = ((String) searchField.getValue()).split(" ");
		if(vals.length!=2)
			return;
		List<DomainUser> list = adConnection.searchUsersInDomain(vals[0], vals[1]);
		dContainer.removeAllItems();
		if(list!=null)
			dContainer.addAll(list);
	}
	
	public void clear(){
		searchField.setValue("");
		dContainer.removeAllItems();
		searchField.focus();
	}
	
	public void addSearchListener(SearchListener listener){
		this.searchListener = listener;
	}
	
	@Override
	public void attach(){
		super.attach();
		this.adConnection = getApplication().getAdConnection();
	}
}
